package com.turing.post.common.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.BlackList;
import com.turing.post.common.util.BlackListBusiness;
import com.turing.post.common.util.BlackListDao;
import com.turing.post.common.util.CommonApplicationException;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.report.util.BinaryFileInfoDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "common.impl.BlackListBusinessImpl")
@Remote
@Local
public class BlackListBusinessImpl implements BlackListBusiness {

	@EJB(mappedName = "common.impl.BlackListDaoImpl")
	private BlackListDao blackListDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "report.impl.BinaryFileInfoDaoImpl")
	private BinaryFileInfoDao binaryFileInfoDaoImpl;

	@Override
	public void insertBlackListInfo(PCPSWebInput webInput, BlackList blackList)
			throws PCPSApplicationException {
		if (blackList.getIdTypeId() == null
				|| PCPSUtil.isEmpty(blackList.getIdNo())
				|| PCPSUtil.isEmpty(blackList.getName())) {
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "黑名单新增失败，需要输入参数项不可为空!");
		}
		// 插入数据库表
		blackListDaoImpl.insertBlackList(blackList);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "创建黑名单", blackList,
				SysOperLog.operType_Add, "remark");
	}

	@Override
	public void updateBlackListInfo(PCPSWebInput webInput, BlackList blackList)
			throws PCPSApplicationException {
		// 待修改实体
		BlackList _blackList = blackListDaoImpl
				.getBlackListByBlackList(blackList);
		if (_blackList == null) {
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "待修改黑名单数据库中不存在!");
		}
		// 对象转换
		_blackList.setNationCodeId(blackList.getNationCodeId());
		_blackList.setName(blackList.getName());
		_blackList.setRemark(blackList.getRemark());
		// 插入数据库表
		_blackList.setIdImageId(blackList.getIdImageId());
		_blackList.setIdImage1Id(blackList.getIdImage1Id());
		blackListDaoImpl.updateBlackList(_blackList);

		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改黑名单", blackList,
				SysOperLog.operType_Edit, "remark");
	}

	@Override
	public void deleteBlackListInfo(PCPSWebInput webInput, BlackList blackList)
			throws PCPSApplicationException {
		if (blackList == null) {
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "传入黑名单为空!");
		}
		BlackList bl = blackListDaoImpl.getBlackListByBlackList(blackList);
		if (bl == null) {
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "需要删除的黑名单不存在!");
		}
		blackListDaoImpl.deleteBlackList(bl);
		if (bl.getIdImageId() != null) {
			// 删除图片文件
			binaryFileInfoDaoImpl.deleteBinaryFileInfoById(bl.getIdImageId());
		}
		if (bl.getIdImage1Id() != null) {
			binaryFileInfoDaoImpl.deleteBinaryFileInfoById(bl.getIdImage1Id());
		}
		// 插入日志
		insertRecordingOperationLog(webInput, "删除黑名单", blackList,
				SysOperLog.operType_Delete, "remark");
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, BlackList obj, String operType, String lastFieldName)
			throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}
}
